package com.example.controller;

import com.example.common.Result;
import com.example.entity.OrganizationBranch;
import com.example.service.OrganizationBranchService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import java.util.List;

/**
 * 组织支部表前端操作接口
 **/
@RestController
@RequestMapping("/organizationBranch")
public class OrganizationBranchController {

    @Resource
    private OrganizationBranchService organizationBranchService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody OrganizationBranch organizationBranch) {
        organizationBranchService.add(organizationBranch);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        organizationBranchService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        organizationBranchService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody OrganizationBranch organizationBranch) {
        organizationBranchService.updateById(organizationBranch);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        OrganizationBranch organizationBranch = organizationBranchService.selectById(id);
        return Result.success(organizationBranch);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(OrganizationBranch organizationBranch) {
        List<OrganizationBranch> list = organizationBranchService.selectAll(organizationBranch);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    /**
     * 分页查询（增加当前用户过滤）
     */
    @GetMapping("/selectPage")
    public Result selectPage(OrganizationBranch organizationBranch,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        // 根据不同的类型返回不同的数据
//        organizationBranch.setOrgType(userType);
        PageInfo<OrganizationBranch> page = organizationBranchService.selectPage(organizationBranch, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 获取领导人管理的支部列表
     */
    @GetMapping("/getLeaderBranches")
    public Result getLeaderBranches(@RequestParam Long leaderId, @RequestParam Integer orgType) {
        List<OrganizationBranch> branches = organizationBranchService.getLeaderBranches(leaderId, orgType);
        return Result.success(branches);
    }

}